const myDomain = "";
const adminUserCookie="bdy_admin";

/**
 * 判断Cookies中是否有用户信息
 * 	仅页面操作用，实际操作中要判断用户是否登录成功
 * @param baseLocation
 * @returns {Boolean}
 */

function isLogin() {
  if (isNotEmpty(getCookie(adminUserCookie))) {
    return true;
  }
  return false;
}

/**
 * 判断Cookies中是否有用户信息
 * 	仅页面操作用，实际操作中要判断用户是否登录成功
 * @param baseLocation
 * @returns {Boolean}
 */

function isLoginAdmin() {
  if (isNotEmpty(getCookie(adminUserCookie))) {
    return true;
  }
  return false;
}

function isNotEmpty(str){
  return !isEmpty(str);
}

function isEmpty(str){
  if(str==null || str=="" || str.trim().length==0){
    return true;
  }
  return false;
}


/**
 * 获取Cookies方法
 * @param cookieName
 * @returns
 */
function getCookie(cookieName) {
  var cookieString = document.cookie;
  var start = cookieString.indexOf(cookieName + '=');
  // 加上等号的原因是避免在某些 Cookie 的值里有
  // 与 cookieName 一样的字符串。
  if (start == -1) // 找不到
    return null;
  start += cookieName.length + 1;
  var end = cookieString.indexOf(';', start);
  if (end == -1) {
    return unescape(cookieString.substring(start));
  } else {
    return unescape(cookieString.substring(start, end));
  }
}


/**
 * 删除Cookies
 * @param name
 */
function DeleteCookie(name) {
  DeleteCookieDomain(name,myDomain);
}



/**创建Cookies
 * @param name
 * @param value
 */
function SetCookie(name, value) {
  SetCookieDomain(name, value,myDomain);
}


/**
 * 删除指定域名下的共享cookie.二级域名可用
 * @param name
 * @param domain
 */
function DeleteCookieDomain(name,domain) {
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = getCookie(name);
  if(isNotEmpty(domain)){
    document.cookie = name + "=" + escape(cval) + ";expires="
      + exp.toGMTString() + ";path=/"+";domain="+domain;
  }else{
    document.cookie = name + "=" + escape(cval) + ";expires="
      + exp.toGMTString() + ";path=/";
  }
}

/**创建Cookies
 * @param name
 * @param value
 */
function SetCookie(name, value) {
  SetCookieDomain(name, value,myDomain);
}

/**
 * 创建Cookies 可设置域名
 * @param name
 * @param value
 * @param domain
 */
function SetCookieDomain(name, value,domain) {
  var Days = 2;
  var exp = new Date();
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  if(isNotEmpty(domain)){
    document.cookie = name + "=" + escape(value) + ";expires="
      + exp.toGMTString() + ";path=/"+";domain="+domain;
  }else{
    document.cookie = name + "=" + escape(value) + ";expires="
      + exp.toGMTString() + ";path=/";
  }
}
